#!/bin/sh

INSTALL_PATH="/usr/local/software/"
NGINX_NUM="nginx-1.12.2"

##安装依赖包
INSTALL_DEVEL(){
    yum install make  pcre pcre-devel apr* autoconf automake curl-devel gcc gcc-c++ zlib-devel openssl openssl-devel pcre-devel gd kernel keyutils patch perl kernel-headers compat* mpfr cpp glibc libgomp libstdc++-devel ppl cloog-ppl keyutils-libs-devel libcom_err-devel libsepol-devel  libselinux-devel krb5-devel zlib-devel libXpm* freetype libjpeg* libpng* php-common php-gd ncurses* libtool* libxml2 libxml2-devel patch -y 
}
##下载安装包
DOWN_PAGE(){
    cd /tmp/
    wget http://nginx.org/download/${NGINX_NUM}.tar.gz
}
 
 ##安装nginx
 INSTALL_PAGE(){
    cd /tmp/
    tar xvf nginx-1.12.2.tar.gz
    cd nginx-1.12.2
    ./configure --prefix=${INSTALL_PATH}${NGINX_NUM} --without-http_memcached_module --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module
    make && make install
 }
 
##修改配置文件
UPDATE_CONF(){
    mkdir -p ${INSTALL_PATH}${NGINX_NUM}/conf/conf.d
    mv ${INSTALL_PATH}${NGINX_NUM}/conf/nginx.conf ${INSTALL_PATH}${NGINX_NUM}/conf/nginx.conf_bak
cat > ${INSTALL_PATH}${NGINX_NUM}/conf/nginx.conf << EOF

user  nginx;
worker_processes  4;

#error_log  logs/error.log  notice;
error_log  logs/error.log  info;

pid    logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    
    default_type  application/octet-stream;
    
    log_format  main  '\$remote_addr - \$remote_user [\$time_local] "\$request" '
                    '\$status \$body_bytes_sent "\$http_referer" '
                    '"\$http_user_agent" "\$http_x_forwarded_for"';
                    
    access_log  logs/access.log  main;
    
    sendfile   on;
    
    keepalive_timeout  65;
    
    include $INSTALL_PATH/$NGINX_NUM/conf/conf.d/*.conf;
}

EOF
cat > ${INSTALL_PATH}${NGINX_NUM}/conf/conf.d/proxy.conf<< EOF
proxy_redirect          off;
proxy_set_header        Host \$host;
proxy_set_header        X-Real-IP \$remote_addr;
proxy_set_header        X-Forwarded-For   \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto http;
proxy_set_header         X-Scheme \$scheme;
client_max_body_size    10m;
client_body_buffer_size 128k;
proxy_connect_timeout   90;
proxy_send_timeout      90;
proxy_read_timeout      90;
proxy_buffer_size       4k;
proxy_buffers           4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
EOF

cat > ${INSTALL_PATH}${NGINX_NUM}/conf/conf.d/default.conf << EOF
#upstream test {
	#ip_hash;
    #server 192.168.8.156:8099    max_fails=3  fail_timeout=60s  ;
    #server 192.168.235.1:8089    max_fails=3  fail_timeout=60s  ;
#}
#server {
#    listen       80;
#    server_name  localhost;
#    location / {
#        proxy_pass http://test/;
#        include ${INSTALL_PATH}${NGINX_NUM}/conf/conf.d/proxy.conf;
#    }
#}

server {
    listen       80;
    server_name  somename  alias  another.alias;
    location / {
        root   html;
        index  index.html index.htm;
    }
}

#server {
#    listen       443 ssl;
#    server_name  localhost;
#
#    ssl_certificate      cert.pem;
#    ssl_certificate_key  cert.key;
#
#    ssl_session_cache    shared:SSL:1m;
#    ssl_session_timeout  5m;
#
#    ssl_ciphers  HIGH:!aNULL:!MD5;
#    ssl_prefer_server_ciphers  on;
#
#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}
#
EOF

cat > /etc/init.d/nginx << EOF
#!/bin/sh
#chkconfig: 2345 90 10
#Short-Description: starts the nginx web server

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

##出错
ERROR(){
    echo -e "\$@  [\033[31mFAILE\033[0m]"
    exit
}
##成功
SUCCEED(){
    echo -e "\$@  [\033[32mOK\033[0m]"
}

PIDFILE=$INSTALL_PATH/$NGINX_NUM/logs/nginx.pid

do_start() {
    $INSTALL_PATH/$NGINX_NUM/sbin/nginx > /dev/null 2>&1 || ERROR "Starting nginx:"
    SUCCEED "Starting nginx:"
}
 
do_stop() {
    kill -INT \`cat \$PIDFILE\` > /dev/null 2>&1 || ERROR "Stopping nginx:"
    SUCCEED "Stopping nginx:"
}
 
do_reload() {
    kill -HUP \`cat \$PIDFILE\` > /dev/null 2>&1 || ERROR "Reloading nginx:"
    SUCCEED "Reloading nginx:"	
}

case "\$1" in
    start)
        do_start
    ;;
    stop)
        do_stop
    ;;
    reload)
        do_reload
    ;;
    restart)
        do_stop
        sleep 2s
        do_start
    ;;
    *)
        ERROR "Usage: /etc/init.d/ {start|stop|reload|restart}"		
    ;;
esac
EOF

    cd /etc/init.d/
    chmod 755 nginx
    /etc/init.d/nginx start       
    chkconfig --add nginx
}